﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using UnityEngine;

namespace template
{
    public class Loader
    {
        public static GameObject load_object;
        public static readonly string Name = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;
        public static readonly string root = @"Unturned_Data/Managed/mods/" + Name + "/";
        public static readonly string cfg = root + Name + ".cfg";
        public static readonly string log = root + Name + ".log";
        public static object[] config;
        public static readonly Dictionary<string, string> defaultConfig = new Dictionary<string, string>()
        {
            //Add new config lines here. The first entry is the line text, and the second is the value type. View supported types below.
            { "Enable: true", "bool" }
        };

        public static void Load()
        {
            
            Directory.CreateDirectory(root);
            Log("Loading " + Name + "...");
            if (!File.Exists(cfg))
            {
                Log(cfg+" not detected, creating anew...");
                System.IO.StreamWriter sw = new StreamWriter(cfg, true);
                foreach (string s in defaultConfig.Keys)
                    sw.WriteLine(Log(s));
                sw.Close();
            }
            else
            {
                Log("Loading config...");
                System.IO.StreamReader sr = new StreamReader(cfg, true);
                List<string> temp = new List<string>();
                while (!sr.EndOfStream)
                {
                    temp.Add(Log(sr.ReadLine()));
                }
                config = new object[temp.Count];
                sr.Close();
                string key = "", value = "";
                for (int i = 0; i < temp.Count; i++)
                {
                    try
                    {
                        key = temp[i].Substring(0, temp[i].IndexOf(": "));
                        value = temp[i].Substring(temp[i].IndexOf(": ") + 2);
                        switch (defaultConfig[temp[i]].ToLower())
                        {
                            //Add new type cases to parse in this switch block.
                            case "bool":
                            case "boolean":
                                config[i] = bool.Parse(value);
                                break;
                            case "int":
                            case "integer":
                                config[i] = int.Parse(value);
                                break;
                            case "short":
                                config[i] = short.Parse(value);
                                break;
                            case "float":
                                config[i] = float.Parse(value);
                                break;
                            case "double":
                                config[i] = double.Parse(value);
                                break;
                            case "byte":
                                config[i] = byte.Parse(value);
                                break;
                            case "long":
                                config[i] = long.Parse(value);
                                break;
                            case "char":
                                config[i] = char.Parse(value);
                                break;
                            case "decimal":
                                config[i] = decimal.Parse(value);
                                break;
                            case "string":
                                config[i] = value;
                                break;
                            default:
                                Log("Invalid type \"" + defaultConfig[key].ToLower() + "\"");
                                throw new Exception();
                        }
                    }
                    catch (Exception)
                    {
                        Log("Invalid entry for: " + key + " type " + defaultConfig[key].ToLower() + ": \"" + value + "\" on line "+(i+1)+".");
                    }
                }
            }
            load_object = new GameObject();
            load_object.AddComponent<Template>();
            UnityEngine.Object.DontDestroyOnLoad(load_object);
            Log(Name + " successfully loaded.");
        }

        public static string Log(string p)
        {
            System.IO.StreamWriter file = new StreamWriter(log, true);
            file.WriteLine("[" + DateTime.Now + "] " + p);
            file.Close();
            return p;
        }

        public static string Log(Exception e)
        {
            System.IO.StreamWriter file = new StreamWriter(log, true);
            while (e.Message != String.Empty)
            {
                file.WriteLine("[" + DateTime.Now + "] " + e.Message);
                e = e.InnerException;
            }
            file.Close();
            return e.Message;
        }

    }

}
